package com.css.fxfzfxqh.modules.vulnerabilityData.verify;

import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import com.css.fxfzfxqh.base.dict.repository.entity.DictItemEntity;
import com.css.fxfzfxqh.base.dict.service.DictService;
import com.css.fxfzfxqh.modules.vulnerabilityData.vo.VulnerabilityImportVo;

import com.css.fxfzfxqh.util.PlatformObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class VulnerabilityExcelVerify implements IExcelVerifyHandler<VulnerabilityImportVo> {

    @Autowired
    DictService dictService;

    @Override
    public ExcelVerifyHandlerResult verifyHandler(VulnerabilityImportVo matManageImportVo) {
        ExcelVerifyHandlerResult result = new ExcelVerifyHandlerResult(true);
        StringBuilder message = new StringBuilder("");



        if (PlatformObjectUtils.isEmpty(matManageImportVo.getLongitude())) {
            result.setSuccess(false);
            message.append("经度 不能为空！");
        }else{
            if(matManageImportVo.getLongitude().scale()>6){
                result.setSuccess(false);
                message.append("经度 小数位数不得超过6位！");
            }
        }

        if (PlatformObjectUtils.isEmpty(matManageImportVo.getLatitude())) {
            result.setSuccess(false);
            message.append("纬度 不能为空！");
        }else{
            if(matManageImportVo.getLatitude().scale()>6){
                result.setSuccess(false);
                message.append("纬度 小数位数不得超过6位！");
            }
        }

        if (PlatformObjectUtils.isEmpty(matManageImportVo.getDamage())) {
            result.setSuccess(false);
            message.append("破坏程度 不能为空！");
        }else{
            if(matManageImportVo.getLatitude().scale()>6){
                List<String> list = checkData("pgcd");
                if(!list.contains(matManageImportVo.getDamage())){
                    result.setSuccess(false);
                    message.append("破坏程度 不正确！");
                }
            }
        }


        if (PlatformObjectUtils.isEmpty(matManageImportVo.getMr())) {
            result.setSuccess(false);
            message.append("mR,D 不能为空！");
        }
        if (PlatformObjectUtils.isEmpty(matManageImportVo.getBr())) {
            result.setSuccess(false);
            message.append("βR,D 不能为空！");
        }

        result.setMsg(message.toString());
        return result;
    }

    private List<String> checkData(String code){
        List<DictItemEntity> validDictItemsByDictCode = dictService.getValidDictItemsByDictCode(code);
        List<String> list = new ArrayList<>();
        for(DictItemEntity dictItemEntity: validDictItemsByDictCode){
            String dictItemName = dictItemEntity.getDictItemName();
            list.add(dictItemName);
        }
        return list;
    }

}
